import React from 'react'
import { inject, observer } from 'mobx-react';
import { withRouter } from 'react-router-dom'
import { Form, RouteLayout } from 'weaver-mobile-page'
import { List, Bubble, Tools, ActivityIndicator} from 'weaver-mobile';
import base from './../../../util/base'
import * as Common from './../../../util/common';
import allStore from "./../../../stores"
import { doSubmitForm, verifyIntervenorInfo } from './../../../util/submit'
import { _ } from './../../../util/pcPublic';
import './../../../style/intervenor.less'

const ButtonGroup = RouteLayout.ButtonGroup;
const FormItem = List.FormItem;
const getLabel = Tools.getLabel;

@inject('wfIntervenor')
@withRouter
@observer
export default class Intervenor extends React.Component {
    componentWillMount() {
        const { setIsShowSingInput } = this.props.wfIntervenor;
        setIsShowSingInput(false)
    }


    getFields = () => {
        const { form } = this.props.wfIntervenor;
        const { conditioninfo, isInit } = form;
        const fields = [];
        if (isInit) {
            conditioninfo.forEach((field) => {
                if (field.domkey == "submitNodeId") {
                    fields.push(<Form.Item
                        key={field.domkey.join('__')}
                        form={form}
                        fieldConfig={field}
                        onChange={this.onChange}
                    />);
                }
            });
            conditioninfo.forEach((field) => {
                if (field.domkey == "enableIntervenor") {
                    fields.push(<Form.Item
                        key={field.domkey.join('__')}
                        form={form}
                        fieldConfig={field}
                        onChange={this.onChange}
                    />);
                }
            });
            conditioninfo.forEach((field) => {
                if (field.domkey == "Intervenorid") {
                    fields.push(<Form.Item
                        key={field.domkey.join('__')}
                        form={form}
                        fieldConfig={field}
                        onChange={this.onChange}
                    />);
                }
            });
            conditioninfo.forEach((field) => {
                if (field.domkey == "SignType") {
                    fields.push(<Form.Item
                        key={field.domkey.join('__')}
                        form={form}
                        fieldConfig={field}
                        onChange={this.onChange}
                    />);
                }
            });
        }
        return fields;
    }

    onChange = (condition, v) => {
        const { form, setNodeName } = this.props.wfIntervenor;
        const { getInterventionUser } = allStore.wfGlobalStore;
        const { changeSubmitParam } = allStore.wfOperateStore;
        if (condition.domkey[0] == 'submitNodeId') {
            if (v) {
                let arr = v.split("_");
                const selnodeid = arr[0];
                const selnodetype = arr[1];
                getInterventionUser({ nodeid: selnodeid, 'nodetype': selnodetype });
            }
            condition.options.length > 0 && condition.options.map(item => {
                if (v === item.key) {
                    setNodeName(item.showname);
                }
            })
        }
        form.onChange(condition, v);
        let updateinfo = {};
        updateinfo[condition.domkey[0]] = v;
        changeSubmitParam({ [condition.domkey[0]]: form.formParams[condition.domkey[0]] });
    }

    render() {
        const { form } = this.props.wfIntervenor;
        const { isInit } = form;
        let buttonGroupData = [
            {
                key: 'clear',
                text: getLabel(201, '取消'),
                onClick: () => {
                    this.props.history.go(-1);
                },
            },
            {
                key: 'submit',
                text: getLabel(826, '确定'),
                onClick: () => {
                    const { nodeName } = this.props.wfIntervenor;
                    const { submitParam } = base.wfOperateStore;
                    const result = verifyIntervenorInfo(submitParam);
                    if (result.type == 0) {
                        Bubble.warning(result.msg, 2);
                        return false;
                    }
                    Common.showConfirm({
                        title: getLabel(504225, "确定将流程干预至") + `[${nodeName}]` + getLabel(504226, "节点吗"),
                        content: "",
                        cancelText: getLabel('31129', '取消'),
                        okText: getLabel('826', '确定'),
                        cancelEvent: () => { },
                        okEvent: () => { doSubmitForm({ actiontype: "requestOperation", src: "intervenor" }) },
                    }, true);
                },
                type: 'primary',
            },
        ]; 
      
        if (isInit) {
            return (
                <div className="wf-monitor-intervenor">
                    <Form className="wf-monitor-intervenor-form">
                        <List>
                            {this.getFields()}
                        </List>
                    </Form>
                    {<div> <ButtonGroup data={buttonGroupData} /> </div>}
                </div>

            )
        } else {
            <div>
                <ActivityIndicator
                    toast
                    text="Loading..."
                    animating={true}
                />
            </div>
        }
    }
}